package com.mls.fivedistrict.controller.portal;

import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Property;
import com.mls.fivedistrict.server.PropertyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Optional;

/**
 * Created by nanji on 2018/3/16.
 */
@RestController
@RequestMapping("/api/app")
public class PropertyController extends BaseController {
    @Resource(name = "propertyService")
    private PropertyService propertyService;

    @GetMapping(path = "/v1/properties")
    public ServiceResponse getPropertyList(HttpServletRequest httpServletRequest) {
        PageFilter pageFilter = getPageFilter();
        pageFilter.getFilterMap().put("userId", getUser().getId() + "");
        ServiceResponse list = propertyService.getProperties(pageFilter);
        return list;
    }

    @GetMapping(path = "/v1/property/{propertyId}")
    public ServiceResponse getProperty(@PathVariable Long propertyId) {
        Optional.ofNullable(propertyId).orElseThrow(() -> new CustomerException("资产不存在"));
        Property property = propertyService.getProperty(propertyId, getUser().getId());
        property.setTotal(property.getTotal().setScale(4, BigDecimal.ROUND_DOWN));
        return ServiceResponse.createSuccessResponse(property);
    }

    @GetMapping(path = "/v1/property/dwq")
    public ServiceResponse getPropertyDwq() {
        Property dwqDetail = propertyService.getDWQDetail(getUser().getId());
        return ServiceResponse.createSuccessResponse(dwqDetail);
    }
}
